import { paginationConfig } from '@/utils'
import { Row, Col, Table } from 'antd'
import { useEffect, useState } from 'react'

const TabTotal = ({ IGDdata, HVdata, CMetricdata, handleExport }) => {
  const [algor1, setAlgor1] = useState('')
  const [algor2, setAlgor2] = useState('')

  const [data, setData] = useState([])


  useEffect(() => {
    if (IGDdata) {
      const algorithmNames = Object.keys(IGDdata) // 获取所有算法的名称
      setAlgor1("MOSL")
      setAlgor2("MOMA")

      let maxResults = 0 // 存储最大的结果数量
      algorithmNames.forEach((algorithm) => {
        const resultKeys = Object.keys(IGDdata[algorithm])
        maxResults = Math.max(maxResults, resultKeys.length)
      })

      // 生成表格数据
      const tData = []
      for (let i = 0;i < maxResults;i++) {
        let key = ''
        if (i === 0) {
          key = `final.txt`
        } else {
          key = `result${i}.txt`
        }
        const value1 = IGDdata[algorithmNames[0]][key].toExponential(4)
        const value2 = IGDdata[algorithmNames[1]][key].toExponential(4)
        const value3 = HVdata[algorithmNames[0]][key].toExponential(4)
        const value4 = HVdata[algorithmNames[1]][key].toExponential(4)
        const value5 = CMetricdata[algorithmNames[0]][key].toExponential(4)
        const value6 = CMetricdata[algorithmNames[1]][key].toExponential(4)
        tData.push({
          id: i,
          igd1: value1,
          igd2: value2,
          hv1: value3,
          hv2: value4,
          cm1: value5,
          cm2: value6,
        })
      }

      setData(tData)
      handleExport(tData, columns)


    }

  }, [IGDdata, HVdata, CMetricdata])

  const columns = [
    {
      title: '序号',
      dataIndex: 'id',
      key: 'id',
      width: 100,
      align: 'center',
      fixed: 'left',
    },
    {
      title: 'IGD',
      children: [
        {
          title: algor1,
          dataIndex: 'igd1',
          align: 'center',
          key: 'igd1',
          width: 150,
          sorter: (a, b) => a.igd1 - b.igd1,
        },
        {
          title: algor2,
          dataIndex: 'igd2',
          align: 'center',
          key: 'igd2',
          width: 150,
          sorter: (a, b) => a.igd2 - b.igd2,
        },
      ],
    },
    {
      title: 'HV',
      children: [
        {
          title: algor1,
          dataIndex: 'hv1',
          align: 'center',
          key: 'hv1',
          width: 150,
          sorter: (a, b) => a.hv1 - b.hv1,
        },
        {
          title: algor2,
          dataIndex: 'hv2',
          align: 'center',
          key: 'hv2',
          width: 150,
          sorter: (a, b) => a.hv2 - b.hv2,
        },
      ],
    },
    {
      title: 'C-metric',
      children: [
        {
          title: algor1,
          dataIndex: 'cm1',
          align: 'center',
          key: 'cm1',
          width: 150,
          sorter: (a, b) => a.cm1 - b.cm1,
        },
        {
          title: algor2,
          dataIndex: 'cm2',
          align: 'center',
          key: 'cm2',
          width: 150,
          sorter: (a, b) => a.cm2 - b.cm2,
        },
      ],
    },
  ]


  return (
    <div>
      {data.length > 0 && <Table
        bordered
        columns={columns}
        dataSource={data}
        scroll={{
          // x: 'max-content',
          y: 'calc(100vh - 460px)',
        }}
        pagination={
          paginationConfig
        }
      >

      </Table>}
    </div>
  )
}

export default TabTotal